package com.bangcommunity.bbframe.monitor.logger;

import com.bangcommunity.bbframe.common.utils.lang.StringUtils;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.slf4j.impl.Log4jAdaperUtil;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tanghc
 * @date 18/1/29
 */
public abstract class Log4jFacility {
    private static Logger createLogger(String mLoggerName, String fileName, String pattern) {
        if (null == pattern) {
            pattern = "%m%n";
        }
        Map<String, Logger> allLoggers = getAllLoggers();
        if (null != allLoggers && null != allLoggers.get(mLoggerName)) {
            return allLoggers.get(mLoggerName);
        }
        Logger logger = Logger.getLogger(mLoggerName);
        logger.removeAllAppenders();
        logger.setLevel(Level.INFO);
        logger.setAdditivity(false);

        DailyRollingFileAppender appender = new DailyRollingFileAppender();
        appender.setAppend(true);
        appender.setDatePattern("'.'yyyyMMdd");
        String filePath = LogFileAccessor.getFilePath(fileName);
        appender.setFile(filePath);
        appender.setName(mLoggerName + "Appender");
        PatternLayout patternOut = new PatternLayout();
        patternOut.setConversionPattern(pattern);
        appender.setLayout(patternOut);
        appender.setThreshold(Level.ERROR);
        appender.activateOptions();
        logger.addAppender(appender);
        return logger;
    }

    public static org.slf4j.Logger createLogger(MLoggerName mLoggerName, String mkey) {
        String pattern = "%m%n";
        switch (mLoggerName) {
        case mlog_stat:
            pattern = "%m%n";
            break;
        case mlog_trace:
            pattern = "[%-d{yyyy-MM-dd HH:mm:ss.SSS}]([%t])[%c][%C:%L]-[%p] - %m%n";
            break;
        }
        return Log4jAdaperUtil
                .createLogger(createLogger(mLoggerName.name(), mLoggerName.getLoggerFileName(mkey), pattern));
    }

    public static Map<String, Logger> getAllLoggers() {
        Map<String, Logger> allLoggers = new HashMap<>();
        try {
            @SuppressWarnings("unchecked")
            Enumeration<Logger> loggers = LogManager.getCurrentLoggers();
            if (loggers != null) {
                while (loggers.hasMoreElements()) {
                    Logger logger = loggers.nextElement();
                    allLoggers.put(logger.getName(), logger);
                }
            }
            // Log4j的root logger要特别处理
            Logger root = LogManager.getRootLogger();
            if (root != null) {
                String name = root.getName();
                Level level = root.getLevel();
                if (level != null && StringUtils.isNotBlank(name)) {
                    LogVoBean log = new LogVoBean();
                    log.setName(root.getName());
                    allLoggers.put(root.getName(), root);
                }
            }
        } catch (Throwable ignore) {
        }
        return allLoggers;
    }

}